package com.czk.classreader.removeinstruction;

import com.czk.utils.FileUtil;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/24 18:07
 */
public class RemoveNopCore {
    public static void main(String[] args) {
        String path = "F:\\IdeaProjects\\asm\\target\\classes\\com\\czk\\sample\\Nop.class";
        byte[] bytes = FileUtil.readBytes(path);
        ClassReader cr = new ClassReader(bytes);
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        ClassVisitor cv = new MethodRemoveNopVisitor(Opcodes.ASM9, cw);
        //（4）结合ClassReader和ClassVisitor
        int parsingOptions = ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
        cr.accept(cv, parsingOptions);

        //（5）生成byte[]
        byte[] bytes2 = cw.toByteArray();

        FileUtil.writeBytes(path, bytes2);
    }
}
